using System;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.Faces.Base;
using <<|=HabaneroUILibrary|>>;
using <<|=BOProjectName|>>;

namespace <<|=ProjectName|>>
{
    /// <summary>
    /// A parent-child grid control used for adding and editing <<|=ClassName+|>> and their <<|=ChildRelationshipName|>>.
    /// </summary>
    public class <<|=ControlName|>> : <<|=UserControlType|>>, IFormControl
    {
        private readonly IControlFactory _controlFactory;

        public <<|=ControlName|>>(IControlFactory controlFactory)
        {
            if (controlFactory == null) throw new ArgumentNullException("controlFactory");
            _controlFactory = controlFactory;

            IReadOnlyGridControl <<|=#ClassName+|>>GridControl = SetupParentControl();
            IReadOnlyGridControl <<|=#ChildRelationshipName|>>GridControl = SetupChildControl();

            // Add the grids to the control. The <<|=ClassName+|>> grid fills the top part of the control.  
            // Change the size here depending on how many items you want to see in this grid.  
            // The <<|=ChildRelationshipName|>> grid fills the rest of the control.
            BorderLayoutManager layoutManager = _controlFactory.CreateBorderLayoutManager(this);
            <<|=#ClassName+|>>GridControl.Height = 200;
            layoutManager.AddControl(<<|=#ChildRelationshipName|>>GridControl, BorderLayoutManager.Position.Centre);
            layoutManager.AddControl(<<|=#ClassName+|>>GridControl, BorderLayoutManager.Position.North, true);

            // Connect the <<|=ClassName+|>> and <<|=ChildRelationshipName|>> grids so that when an item is selected
            // in the <<|=ClassName+|>> grid, the correct <<|=ChildRelationshipName|>> are shown in the child grid.
            <<|=#ClassName+|>>GridControl.Grid.BusinessObjectSelected += 
                delegate
                    {
                        <<|=ClassName|>> selected<<|=ClassName|>> = <<|=#ClassName+|>>GridControl.Grid.SelectedBusinessObject as <<|=ClassName|>>;
                        if (selected<<|=ClassName|>> == null) return;
                        <<|=#ChildRelationshipName|>>GridControl.BusinessObjectCollection = selected<<|=ClassName|>>.<<|=ChildRelationshipName|>>;
                    };
        }

        private IReadOnlyGridControl SetupParentControl()
        {
            // Create the <<|=ClassName+|>> read-only grid
            IReadOnlyGridControl <<|=#ClassName+|>>GridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            <<|=#ClassName+|>>GridControl.Initialise(ClassDef.Get<<<|=ClassName|>>>(), "<<|=UIDefName|>>");

            // Load the collection of <<|=ClassName+|>>.  You can set the criteria and order by values here.
            <<|=#ClassName+|>>GridControl.BusinessObjectCollection = Broker.GetBusinessObjectCollection<<<|=ClassName|>>>("", "");

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            <<|=#ClassName+|>>GridControl.FilterControl.Visible = false;
            //<<|=#ClassName+|>>GridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //<<|=#ClassName+|>>GridControl.Buttons.ShowDefaultDeleteButton = true;

            return <<|=#ClassName+|>>GridControl;
        }

        private IReadOnlyGridControl SetupChildControl()
        {
            // Create the <<|=ChildRelationshipName|>> read-only grid to show the child items
            IReadOnlyGridControl <<|=#ChildRelationshipName|>>GridControl = _controlFactory.CreateReadOnlyGridControl();

            // Used to specify a custom UI def
            <<|=#ChildRelationshipName|>>GridControl.Initialise(ClassDef.Get<<<|=ChildClassName|>>>(), "<<|=ChildUIDefName|>>");

            // Creates an empty collection of <<|=ChildRelationshipName|>> to put in the child grid
            <<|=#ChildRelationshipName|>>GridControl.BusinessObjectCollection = new BusinessObjectCollection<<<|=ChildClassName|>>>();

            // Set this to true to enable the filters, then add your filters below (see commented out line)
            <<|=#ChildRelationshipName|>>GridControl.FilterControl.Visible = false;
            //<<|=#ChildRelationshipName|>>GridControl.FilterControl.AddStringFilterTextBox("", "");

            // Uncomment this line to enable the default delete button
            //<<|=#ChildRelationshipName|>>GridControl.Buttons.ShowDefaultDeleteButton = true;

            return <<|=#ChildRelationshipName|>>GridControl;
        }

        /// <summary>
        /// Called whenever the control is placed on a form by the menu system.  Put any initialisation
        /// code in here that must be run whenever the form is displayed.
        /// </summary>
        public void SetForm(IFormHabanero form) { }
    }
}
